package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.DischargeInfoVO;
import com.ruoyi.system.domain.vo.TAdmissionRegistrationVo;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 住院登记Controller
 * 
 * @author 杨一迪
 * @date 2024-12-29
 */
@Controller
@RequestMapping("/system/registration")
public class TAdmissionRegistrationController extends BaseController
{
    private String prefix = "system/registration";
    @Resource
    private ITWardService wardService;
    @Resource
    private ITBedService bedService;

    @Resource
    private ITDoctorService doctorService;

    @Autowired
    private ITPatientService tPatientService;

    @Resource
    private ISysDeptService deptService;

    @Autowired
    private ITNurseService nurseService;

    @Autowired
    private ITAdmissionRegistrationService tAdmissionRegistrationService;

    @RequiresPermissions("system:registration:view")
    @GetMapping()
    public String registration(ModelMap mmap)
    {
        List<TPatient> tPatients = tPatientService.selectTPatientList(new TPatient());
        mmap.put("tPatients",tPatients);
        return prefix + "/registration";
    }

    @PostMapping("/getDischargeInfo")
    @ResponseBody
    public AjaxResult getDischargeInfo(Long id) {
        TAdmissionRegistration tAdmissionRegistration = tAdmissionRegistrationService.selectTAdmissionRegistrationById(id);
        Long bedId = tAdmissionRegistration.getBedId();
        Long doctorId = tAdmissionRegistration.getDoctorId();
        Long nurseId = tAdmissionRegistration.getNurseId();
        Long patientId = tAdmissionRegistration.getPatientId();
        Long wardId = tAdmissionRegistration.getWardId();
        TPatient tPatient = tPatientService.selectTPatientById(patientId);
        DischargeInfoVO info = DischargeInfoVO.builder()
                .bedNumber(bedService.selectTBedById(bedId).getBedNumber())
                .doctorName(doctorService.selectTDoctorById(doctorId).getName())
                .nurseName(nurseService.selectTNurseById(nurseId).getName())
                .patientName(tPatient.getName())
                .age(String.valueOf(tPatient.getAge()))
                .idNumber(tPatient.getIdNumber())
                .wardNumber(wardService.selectTWardById(wardId).getWardNumber())
                .deptName(deptService.selectDeptById(tPatient.getDepartmentId()).getDeptName())
                .admissionDate(tAdmissionRegistration.getAdmissionDate())
                .build();
        return AjaxResult.success(info);
    }


    @PostMapping("/completeDischarge")
    @ResponseBody
    public AjaxResult completeDischarge(Long id) {
        // 业务逻辑包含：
        // 1. 更新住院登记状态为出院
        TAdmissionRegistration tAdmissionRegistration = tAdmissionRegistrationService.selectTAdmissionRegistrationById(id);
        tAdmissionRegistration.setStatus(1L);
        // 2. 设置支付状态为已支付
        tAdmissionRegistration.setPaymentStatus(1);
        tAdmissionRegistrationService.updateTAdmissionRegistration(tAdmissionRegistration);
        // 3. 释放床位资源
        TBed tBed = bedService.selectTBedById(tAdmissionRegistration.getBedId());
        tBed.setIsAvailable(0);
        bedService.updateTBed(tBed);
        // 4. 记录实际出院日期

        return AjaxResult.success("已释放病床信息~");
    }
    /**
     * 查询住院登记列表
     */
    @RequiresPermissions("system:registration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TAdmissionRegistration tAdmissionRegistration)
    {
        startPage();
        List<TAdmissionRegistration> list = tAdmissionRegistrationService.selectTAdmissionRegistrationList(new TAdmissionRegistration());
        List<TAdmissionRegistrationVo> admissionRegistrationVos = new ArrayList<>();
        list.forEach(ars -> {
            TAdmissionRegistrationVo tAdmissionRegistrationVo = new TAdmissionRegistrationVo();
            BeanUtils.copyProperties(ars,tAdmissionRegistrationVo);
            TWard tWard = wardService.selectTWardById(ars.getWardId());
            TBed tBeds = bedService.selectTBedById(ars.getBedId());
            TDoctor tDoctor = doctorService.selectTDoctorById(ars.getDoctorId());
            TNurse tNurse = nurseService.selectTNurseById(ars.getNurseId());
            TPatient tPatient = tPatientService.selectTPatientById(ars.getPatientId());

            tAdmissionRegistrationVo.setDoctorName(tDoctor.getName());
            tAdmissionRegistrationVo.setBedName(tBeds.getBedNumber());
            tAdmissionRegistrationVo.setWardName(tWard.getWardNumber());
            tAdmissionRegistrationVo.setNurseName(tNurse.getName());
            tAdmissionRegistrationVo.setPatientName(tPatient.getName());

            admissionRegistrationVos.add(tAdmissionRegistrationVo);
        });
        return getDataTable(admissionRegistrationVos);
    }

    /**
     * 导出住院登记列表
     */
    @RequiresPermissions("system:registration:export")
    @Log(title = "住院登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TAdmissionRegistration tAdmissionRegistration)
    {
        List<TAdmissionRegistration> list = tAdmissionRegistrationService.selectTAdmissionRegistrationList(tAdmissionRegistration);
        ExcelUtil<TAdmissionRegistration> util = new ExcelUtil<TAdmissionRegistration>(TAdmissionRegistration.class);
        return util.exportExcel(list, "住院登记数据");
    }

    /**
     * 新增住院登记
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TPatient> tPatients = tPatientService.selectTPatientList(new TPatient());
        TWard tWard = new TWard();
        tWard.setStatus(0L);
        List<TWard> tWards = wardService.selectTWardList(tWard);
        TBed tBed = new TBed();
        tBed.setIsAvailable(0);
        List<TBed> tBeds = bedService.selectTBedList(tBed);
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        List<TNurse> tNurses = nurseService.selectTNurseList(new TNurse());
        mmap.put("tPatients",tPatients);
        mmap.put("tWards",tWards);
        mmap.put("tBeds",tBeds);
        mmap.put("tDoctors",tDoctors);
        mmap.put("tNurses",tNurses);
        return prefix + "/add";
    }

    /**
     * 新增保存住院登记
     */
    @RequiresPermissions("system:registration:add")
    @Log(title = "住院登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TAdmissionRegistration tAdmissionRegistration)
    {
        return toAjax(tAdmissionRegistrationService.insertTAdmissionRegistration(tAdmissionRegistration));
    }

    /**
     * 修改住院登记
     */
    @RequiresPermissions("system:registration:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TAdmissionRegistration tAdmissionRegistration = tAdmissionRegistrationService.selectTAdmissionRegistrationById(id);
        mmap.put("tAdmissionRegistration", tAdmissionRegistration);
        List<TPatient> tPatients = tPatientService.selectTPatientList(new TPatient());
        TWard tWard = new TWard();
        tWard.setStatus(0L);
        List<TWard> tWards = wardService.selectTWardList(tWard);
        TBed tBed = new TBed();
        tBed.setIsAvailable(0);
        List<TBed> tBeds = bedService.selectTBedList(tBed);
        List<TDoctor> tDoctors = doctorService.selectTDoctorList(new TDoctor());
        List<TNurse> tNurses = nurseService.selectTNurseList(new TNurse());
        mmap.put("tPatients",tPatients);
        mmap.put("tWards",tWards);
        mmap.put("tBeds",tBeds);
        mmap.put("tDoctors",tDoctors);
        mmap.put("tNurses",tNurses);
        return prefix + "/edit";
    }

    /**
     * 修改保存住院登记
     */
    @RequiresPermissions("system:registration:edit")
    @Log(title = "住院登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TAdmissionRegistration tAdmissionRegistration)
    {
        return toAjax(tAdmissionRegistrationService.updateTAdmissionRegistration(tAdmissionRegistration));
    }

    /**
     * 删除住院登记
     */
    @RequiresPermissions("system:registration:remove")
    @Log(title = "住院登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tAdmissionRegistrationService.softDeleteTAdmissionRegistrationByIds(ids));
    }
}
